package com.han.walkman.controller;

import com.han.walkman.entity.ProductionEntity;
import com.han.walkman.service.ProductionService;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: Hanjiafeng
 * @Date: 2020/5/2
 * @Desc:
 */
@RestController
@RequestMapping("/production")
public class ProductionController {
    private final ProductionService productionService;

    public ProductionController(ProductionService productionService) {
        this.productionService = productionService;
    }

    /**
     * 分页查询
     *
     * @return list
     */
    @GetMapping("/findAll")
    public List<ProductionEntity> findAll() {
        return productionService.findAll();
    }

    /**
     * 按照产品类型分类返回
     *
     * @return 分类后的产品
     */
    @GetMapping("/findAllSortByType")
    public List<List<ProductionEntity>> findAllSortByType() {
        List<ProductionEntity> productionEntityList = findAll();
        Map<Integer, List<ProductionEntity>> sortedMap = new HashMap<>(1);
        for (ProductionEntity entity : productionEntityList) {
            Integer type = entity.getTypeId();
            List<ProductionEntity> tempList;
            if (sortedMap.containsKey(type)) {
                tempList = sortedMap.get(type);
            } else {
                tempList = new ArrayList<>();
            }
            tempList.add(entity);
            sortedMap.put(type, tempList);
        }
        List<List<ProductionEntity>> sortedList = new ArrayList<>(sortedMap.values());
        sortedList.add(0, productionEntityList);
        return sortedList;
    }

    /**
     * 分页查询
     *
     * @return list
     */
    @GetMapping("/findByKeyword")
    public Page<ProductionEntity> findByPage(String keyword, Integer type, Integer index, Integer size) {

        //构建搜索条件
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                //忽略大小写
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        ProductionEntity entity = new ProductionEntity();
        if (type != 0) {
            entity.setTypeId(type);
        }
        entity.setName(keyword);
        Example<ProductionEntity> entityExample = Example.of(entity, exampleMatcher);

        Pageable pageable = PageRequest.of(index, size);
        return productionService.findAll(entityExample, pageable);
    }

    @GetMapping("/findById")
    public ProductionEntity findById(Integer id) {
        Optional<ProductionEntity> entityOptional = productionService.findById(id);
        return entityOptional.orElse(null);
    }

    /**
     * 查询产品轮播图
     *
     * @return 剔除掉不需要参数的轮播图list
     */
    @GetMapping("/findStarProduction")
    public List<ProductionEntity> findStarProduction() {
        ProductionEntity entity = new ProductionEntity();
        entity.setStar(true);
        Example<ProductionEntity> entityExample = Example.of(entity);
        return productionService.findAll(entityExample);
    }
}
